package com.xie.o2o.controller.shopadmin;

import com.google.common.collect.Maps;
import com.xie.o2o.dto.ProductCategoryExecution;
import com.xie.o2o.dto.Result;
import com.xie.o2o.entity.ProductCategory;
import com.xie.o2o.entity.Shop;
import com.xie.o2o.enums.ProductCategoryStateEnum;
import com.xie.o2o.service.ProductCategoryService;
import com.xie.o2o.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @create: xie
 */
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManageController {
    private final static Logger log = LoggerFactory.getLogger(ProductCategoryManageController.class);
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 查询店铺下所有商品分类
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getproductcategorylist", method = RequestMethod.GET)
    @ResponseBody
    private Result<List<ProductCategory>> getShopManagementInfo(HttpServletRequest request) {
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        List<ProductCategory> list;
        if (currentShop != null && currentShop.getShopId() >= 0) {
            list = productCategoryService.queryProductCategoryList(currentShop.getShopId());
            return new Result<List<ProductCategory>>(true, list);
        } else {
            ProductCategoryStateEnum inner_error = ProductCategoryStateEnum.INNER_ERROR;
            return new Result<List<ProductCategory>>(false, inner_error.getStateInfo(), inner_error.getState());
        }
    }

    /**
     * 批量添加商品类别
     *
     * @param productCategoryList
     * @param request
     * @return
     */
    @RequestMapping(value = "/addproductcategory", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> addProductCategory(@RequestBody List<ProductCategory> productCategoryList,
                                                   HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        if (productCategoryList != null && productCategoryList.size() > 0) {
            try {
                for (ProductCategory pc : productCategoryList) {
                    pc.setShopId(currentShop.getShopId());
                }
                productCategoryService.batchAddProductCategoryList(productCategoryList);
                map.put("success", true);
            } catch (RuntimeException e) {
                log.error("添加商品分类错误:"+e.toString());
                throw new RuntimeException("添加商品分类错误");
            }
        } else {
            map.put("success", false);
            map.put("errMsg", "请至少输入一个商品类别");
        }
        return map;
    }

    /**
     * 删除商品分类
     *
     * @param productCategoryId 传入的分类id
     * @param request
     * @return
     */
    @RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> removeProductCategory(Long productCategoryId,
                                                      HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        if (productCategoryId != null && productCategoryId > 0) {
            try {
                Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
                productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
                map.put("success", true);
            } catch (RuntimeException e) {
                log.error("删除商品分类错误:"+e.toString());
                throw new RuntimeException("删除商品分类错误");
            }
        } else {
            map.put("success", false);
            map.put("errMsg", "请至少输入一个商品类别");
        }
        return map;
    }

}
